// pages/ss/ssindex/ssindex.js

const app = getApp();
var funcs = require('../../../static/js/funcs.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    category_list:["贷款列表","提醒列表"],
    select_category:0,
    ss_bodys:[{"checkedFlag":true},{"checkedFlag":false},{"checkedFlag":false},{"checkedFlag":false}],
    grouplist:[],
    bl_type: 1,
    bl_status:"正常",
  },
  select_category:function(e){
    let index = e.currentTarget.dataset.category
    console.log(index)
    let that = this
    this.setData({
      select_category: index
    })
    if(index==0){
      this.freshList()
    }else{
      this.freshTxList()
    }
  },

  switch1Change(e){
    let flag = e.detail.value
    let openid = app.globalData.openid
    let that = this
    if(flag==true){
      console.log("开启开关")
      wx.requestSubscribeMessage({
        tmplIds: ['BiP7HBKMoj24OhFZwj6LAmbuT5Kp2omjuBKqxhhTNdM'],
        success: (res) => {
          console.log("success回调", res)
   
          if(res.BiP7HBKMoj24OhFZwj6LAmbuT5Kp2omjuBKqxhhTNdM!=null){
            if(res.BiP7HBKMoj24OhFZwj6LAmbuT5Kp2omjuBKqxhhTNdM.includes("accept")){
                console.log("用户确实点了接受")
                that.setData({
                  Checkeddisabled: true
                })
            }else{
              that.setData({
                showChecked: true,
              })
            }
            //无论接受与否，都记录一下，避免遗漏，后面可找用户手动操作
            funcs.addToNotes(openid,this.data.staffOanum,this.data.staffName).then(res=>{
              if(res.data.succeed){
                console.log("添加到提醒列表成功")
              }else{
                console.log("添加到提醒列表失败")
              }
            })
          }
        },
        fail: res => {
          console.log('fail回调', res)
          this.setData({
            Checkeddisabled: false
          })
        }
      })
    }else{
      console.log("关闭开关")
    }
    // console.log(e.detail.value)
  },
  freshTxList(){
    let that = this
    let openid = app.globalData.openid
    funcs.getLigListWithTix(openid,this.data.staffOanum,1).then(res=>{
      if(res.data.succeed){
        console.log(res.data.data.lgList)
        that.setData({
          ligTxList:res.data.data.lgList
        })
      }else{
        console.log("获取提醒列表失败")
      }

    })
  },
  freshList(){
    let that = this
    let openid = app.globalData.openid
    funcs.getLigList(openid,this.data.staffOanum,this.data.bl_status).then(res=>{
      if(res.data.succeed){
        console.log(res.data.data.ligList)
        that.setData({
          ligList:res.data.data.ligList
        })
      }else{
        console.log("获取不良列表失败")
      }

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    let openid = app.globalData.openid
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    funcs.judgeStaff(openid).then(res=>{
      if(res.data.succeed){
        console.log(res.data.data)
        console.log("该员工已注册")
        that.setData({
          staffName:res.data.data.name,
          staffOanum: res.data.data.oanum,
          staffPhone: res.data.data.telnum
        })
        funcs.getGroup(openid).then(res=>{
          if(res.data.succeed){
            let grouplist = res.data.data.groups
            console.log(grouplist)
            if(grouplist.length>0){
              that.setData({
                grouplist: grouplist,
                selectedgroup: grouplist.length>0? grouplist[0].groupId: null,
              })  
            
              funcs.getLigList(openid,that.data.staffOanum,this.data.bl_status).then(res=>{
                if(res.data.succeed){
                  console.log(res.data.data.ligList)
                  that.setData({
                    ligList:res.data.data.ligList
                  })
                  wx.getSetting({
                    withSubscriptions: true,
                    success (res) {
                      console.log(res.authSetting)
                      console.log(res)
                      if(res.subscriptionsSetting.BiP7HBKMoj24OhFZwj6LAmbuT5Kp2omjuBKqxhhTNdM!=null&&res.subscriptionsSetting.BiP7HBKMoj24OhFZwj6LAmbuT5Kp2omjuBKqxhhTNdM.includes("accept")){
                        that.setData({
                          showChecked: false,
                          Checkeddisabled: true
                        })
                      }else{
                        that.setData({
                          showChecked: true
                        })
                      }
                    }
                  })
                  wx.hideLoading()
                }else{
                  console.log("获取不良列表失败")
                  wx.hideLoading()
                }

              })
            }
          }else{
            console.log("获取群组失败")
            wx.hideLoading()
          }
        })
      }else{
        app.globalData.loginedFlag = false
        wx.reLaunch({
          url: '/pages/my/my',
        })
        wx.showToast({
          title: "请先绑定",
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  addNew(e){
    wx.navigateTo({
      url: '/pages/ss/addnew/addnew',
    })
    // this.errorNotice()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  errorNotice: function (res) {
    wx.showToast({
      title: "因数据安全考虑\r\n目前暂不开放该功能",
      icon: 'none',
      duration: 2000
    })
  },

  showAlready(){
    wx.showToast({
      title: "消息提醒已开启",
      icon: 'none',
      duration: 2000
    })
  },
  radioChange(e) {
    let openid = app.globalData.openid
    let value = e.detail.value;
    this.setData({
      bl_status: value,
      bl_type: value=="正常"? 1:(value=="风险"? 2:3)
    })
    console.log(this.data.bl_status)
    this.freshList()

  },
  gotoDetail(e){
    let uid = e.currentTarget.dataset.uid
    let data = e.currentTarget.dataset; 
    console.log(uid)
    console.log(data)
    var jsonString = JSON.stringify(data);
    wx.navigateTo({
      url: '/pages/ss/modify/modify?params=' + encodeURIComponent(jsonString)
    })
  },
  todoMark(e){
    let openid = app.globalData.openid
    let uid = e.currentTarget.dataset.uid
    let status = e.currentTarget.dataset.status
    console.log(status)
    console.log(uid)
    let that = this
    wx.showModal({
      title: '提示',
      content: status=="正常"? '确定标记为风险吗？':'确定标记为不良吗？',
      success(res) {
        if (res.confirm) {
          funcs.updateLgStatus(openid,uid,status=="正常"? "风险":"不良").then(res=>{
            if(res.data.succeed){
              that.freshList()
              that.freshTxList()
            }
          })
        }
      }
    })
  },

  clickTix(e){
    let uid = e.currentTarget.dataset.uid
    let tflag =  e.currentTarget.dataset.tflag  //当前提醒按钮状态
    let openid = app.globalData.openid
    let that = this
    console.log(uid)
    funcs.updateTix(openid,uid,tflag,this.data.staffOanum).then(res=>{
      if(res.data.succeed){
        console.log("更新成功")
        that.freshList()
        that.freshTxList()
        wx.showToast({
          title: tflag==0? '添加提醒成功':'取消提醒成功',
          icon: "success",
          duration: 2000
        })
      }
    })
  }
})